#!/usr/bin/env python

import sys
#import simplejson, json
from os3.utils import simplejson
from gallery_class import Gallery

def init ( liwe, admin = 0 ):
        from liwe import parts

	Gallery ( liwe )

        parts.add ( liwe.layman, "ext",  )
        parts.add_wwl ( liwe.layman, [ "gallery" ] )
	"""
        liwe.layman.add_css ( "modules/gallery/gfx/gallery.css" )
	"""
        liwe.layman.add_js ( "modules/gallery/js/gallery.js" )
        liwe.layman.add_js_code ( "gallery.init ();" )
        if admin and int ( admin ):
                liwe.layman.add_css ( "modules/gallery/gfx/gallery.admin.css" )
                liwe.layman.add_js ( "modules/gallery/js/gallery.admin.js" )
                liwe.layman.add_js_code ( "gallery.admin.init ();" )

	return ''

def gallery_box ( liwe, dest_div = '', id_gal = 0, wwl_name = 'wwl-gallery' ):
	ga = Gallery ( liwe )

	img_res = ga.list_imgs ( id_gal = id_gal ) 

	for index, row in enumerate ( img_res [ 'rows' ] ):
		del row [ 'created' ]
		del row [ 'shot_date' ]
		img_res [ 'rows' ][ index ] = row

	res_full = {	'items' : img_res [ 'rows' ], 
			'image_path': ga.cfg.get ( 'image.path', 'site/gallery/images' ), 
			'thumb_path': ga.cfg.get ( 'thumb.path', 'site/gallery/thumbs' ),
			'thumb_size': img_res [ 'thumb_size' ]
			}
	res_to_func = simplejson.dumps ( res_full );	
	
        liwe.layman.add_js_code ( "gallery._gallery_box ( '%s', '%s', '%s' )" % ( dest_div, wwl_name, res_to_func ) )
		
